package com.daka.service.impl;

import com.daka.dao.ActivityApplyDao;
import com.daka.entity.Activity;
import com.daka.entity.ActivityApply;
import com.daka.entity.Member;
import com.daka.enumerate.StatusCode;
import com.daka.service.ActivityApplyService;
import com.daka.service.ActivityService;
import com.daka.service.MemberService;
import com.daka.tools.DKQConstant;
import com.daka.tools.DateUtil;
import com.daka.tools.ServiceRuntimeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 16:40
 */

@Service("activityApplyService")
@Transactional
public class ActivityApplyServiceImpl implements ActivityApplyService {

    @Autowired
    private ActivityApplyDao activityApplyDao;
    @Autowired
    private MemberService memberService;
    @Autowired
    private ActivityService activityService;

    @Override
    public int insert(ActivityApply po) {

        return activityApplyDao.insert(po);
    }

    @Override
    public int update(ActivityApply po) {
        return activityApplyDao.updateByPrimaryKeySelective(po);
    }

    @Override
    public ActivityApply selectByPrimaryKey(Integer id) {
        return activityApplyDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ActivityApply> selectByStatement(ActivityApply po) {
        return activityApplyDao.selectByStatement(po);
    }

    @Override
    public int joinActivity(String activityCode, String memberID) {

        Member member = memberService.selectByMemberId(memberID);
        if(member==null ){
            throw new ServiceRuntimeException(StatusCode.VOLUNTEER_NOT_EXISTS.getCode(),StatusCode.VOLUNTEER_NOT_EXISTS.getMessage());
        }
        Activity activity = new Activity();
        activity.setActivityCode(activityCode);
        List<Activity> listAc = activityService.selectByStatement(activity);
        if(listAc==null || listAc.size()<1){
            throw new ServiceRuntimeException(StatusCode.ACTIVITY_NOT_EXISTS.getCode(),StatusCode.ACTIVITY_NOT_EXISTS.getMessage());
        }
        activity = listAc.get(0);
        //检查活动报名名额是否已满
        if(activity.getNeeds()>0) {
            int applys = this.activityApplyDao.countActApply(activity.getId());
            if(applys>= activity.getNeeds())
                throw new ServiceRuntimeException(StatusCode.ACTIVITY_FULL_APPLY.getCode(),StatusCode.ACTIVITY_FULL_APPLY.getMessage());
        }
        //检查当前用户是否已报过名,并且各个活动不能有时间上的重叠
        String start = activity.getStartDay()+" "+activity.getStartHour()+":"+activity.getStartMinute()+":00";
        String end = activity.getEndDay()+" "+activity.getEndHour()+":"+activity.getEndMinute()+":00";
        List<Activity> data = activityService.selectApplyByUser(member.getId());
        if(null != data && data.size()>0) {
            for(Activity aa:data) {
                if(aa.getDelStatus() == DKQConstant.DEL_NO )
                    if(DateUtil.isOverLaps(aa.getStartDay()+" "+aa.getStartHour()+":"+aa.getStartMinute(), aa.getEndDay()+" "+aa.getEndHour()+":"+aa.getEndMinute(),
                            activity.getStartDay()+" "+activity.getStartHour()+":"+activity.getStartMinute(),
                            activity.getEndDay()+" "+activity.getEndHour()+":"+activity.getEndMinute(), "yyyy-MM-dd HH:mm") == true) {
                        throw new ServiceRuntimeException(StatusCode.ACTIVITY_TIME_OVERLAP.getCode(),StatusCode.ACTIVITY_TIME_OVERLAP.getMessage());
                    }
            }
        }
        ActivityApply activityApply = new ActivityApply();
        activityApply.setMember(member.getId());
        activityApply.setActivity(activity.getId());
        List<ActivityApply> list = activityApplyDao.selectByStatement(activityApply);
        if(list!=null && list.size()>0){
            //曾经加入过，则修改状态
            activityApply = list.get(0);
            activityApply.setStatus(DKQConstant.APPLY_STATUS_OK);
            activityApplyDao.updateByPrimaryKeySelective(activityApply);
        }
        activityApply.setMember(member.getId());
        activityApply.setActivity(activity.getId());
        activityApply.setUserType(DKQConstant.ROLES_VOLUNTEER);
        activityApply.setStatus(DKQConstant.APPLY_STATUS_OK);
        activityApply.setDelstatus(DKQConstant.DEL_NO);
        activityApply.setCreatetime(DateUtil.getDate());
        activityApplyDao.insert(activityApply);
        //更新活动的报名人数
        activity.setAttendCount(activity.getAttendCount() + 1);
        activityService.update(activity);
        return 1;
    }

    @Override
    public int quit(String activityCode, String memberID) {
        Member member =  memberService.selectByMemberId(memberID);
        if(member==null ){
            throw new ServiceRuntimeException(StatusCode.VOLUNTEER_NOT_EXISTS.getCode(),StatusCode.VOLUNTEER_NOT_EXISTS.getMessage());
        }

        Activity activity = new Activity();
        activity.setActivityCode(activityCode);
        List<Activity> listAc = activityService.selectByStatement(activity);
        if(listAc==null || listAc.size()<1){
            throw new ServiceRuntimeException(StatusCode.ACTIVITY_NOT_EXISTS.getCode(),StatusCode.ACTIVITY_NOT_EXISTS.getMessage());
        }

        activity = listAc.get(0);
        //判断是否参加活动
        ActivityApply activityApply = new ActivityApply();
        activityApply.setMember(member.getId());
        activityApply.setActivity(activity.getId());
        List<ActivityApply> list = activityApplyDao.selectByStatement(activityApply);
        if(list==null || list.size()<1){
            throw new ServiceRuntimeException(StatusCode.ACTIVITY_NOT_APPLY.getCode(),StatusCode.ACTIVITY_NOT_APPLY.getMessage());
        }

        activityApply=list.get(0);
        activityApply.setStatus(DKQConstant.APPLY_STATUS_EXIT);
        activityApplyDao.updateByPrimaryKeySelective(activityApply);
        //更新活动的报名人数
        if(activity.getAttendCount()>0){
            activity.setAttendCount(activity.getAttendCount()-1);
            activityService.update(activity);
        }
        return 1;
    }
}
